$(function () {
    //清空購物車
    $('.h-right').on('click', function () {
        var _this = $(this);
        Magiclamp.mcConfirm(null, "确定清空购物车么?", null, function () {
            var url = _this.data('href');
            Magiclamp.mcAjax(url, "GET", '', function () {
                location.reload();
            }, function () {
            });
        });
        return false;
    });


    //数量调整控件，更新合集價格
    Magiclamp.quantityCtrl(cart_total_price, cart_total_price);

    //动态计算购物车总价
    cart_total_price();
    function cart_total_price() {
        var total_money = 0;
        $('.list-price').each(function (i, obj) {
            var keyid = $(this).data('keyid');
            if ($("#like-chkbox-" + keyid).is(":checked")) {
                var price = parseInt($(obj).html() * 100);
                var count = $("#count_" + keyid).html() * 1;
                total_money += price * count;
            }
        });
        total_money = total_money / 100;
        $("#total_money").html(total_money.toFixed(2));
    }

    $(".cart-checkbox,#chk-all").on('click', function () {
        cart_total_price();
    });


    $("#gotopay").on('click', function () {
        $(this).css('pointer-events', 'none');
        var data = $("#cart_form").serialize();
        $(".list-sum").each(function (i, obj) {
            var kid = $(obj).attr('id');
            var value = parseInt($(obj).html());
            data += '&' + kid + '=' + value;
        });
        var url = $(this).data('href');
        Magiclamp.mcAjax(url, "POST", data, function (data) {
            $("#gotopay").css('pointer-events', 'auto');
            if (parseInt(data.status) === 1) {
                var paycode = data.data.paycode;
                if(data.data.is_service_price.dn_send_min_price!==undefined){
                    var dn_send_min_price=data.data.is_service_price.dn_send_min_price/100;
                    var dn_service_price=data.data.is_service_price.dn_service_price/100;
                    var allprice=data.data.is_service_price.allprice/100;
                    var message='您购买商品总价为'+allprice.toFixed(2)+'元,未超过起送费用'+dn_send_min_price.toFixed(2)+'元';
                    message+='将收取服务费'+dn_service_price.toFixed(2)+'元，确认继续支付，取消继续选购商品';
                    Magiclamp.mcConfirm('提示',message,function () {
                        return false;
                    },function () {
                        location.href = "/pay/main/paytype/" + paycode;
                    })
                }else {
                    location.href = "/pay/main/paytype/" + paycode;
                }

            } else {
                Magiclamp.mcAlert('', data.message);
            }
        }, function () {
            $("#gotopay").css('pointer-events', 'auto');
        });
        return false;
    });
});